package com.ufo.store.management.ui.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.app.Fragment;
import android.support.v4.util.Pair;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ufo.store.management.R;
import com.ufo.store.management.adapter.Isszmbalv3StockOrMemberRecyclerViewAdapter;
import com.ufo.store.management.adapter.Isszmbalv3StockTypeRecyclerViewAdapter;
import com.ufo.store.management.database.DatabaseManager;
import com.ufo.store.management.entity.Isszmbalv3Member;
import com.ufo.store.management.entity.Isszmbalv3StockInfo;
import com.ufo.store.management.entity.SerializableList;
import com.ufo.store.management.entity.SerializableMap;
import com.ufo.store.management.ui.activity.Isszmbalv3MemberConsumptionActivity;
import com.ufo.store.management.ui.activity.Isszmbalv3MemberEditActivity;
import com.ufo.store.management.ui.activity.Isszmbalv3StockActivity;
import com.ufo.store.management.ui.activity.Isszmbalv3StockEditActivity;
import com.ufo.store.management.ui.activity.Isszmbalv3StockSalesActivity;
import com.ufo.store.management.utils.BitmapUtils;
import com.ufo.store.management.utils.JavaBlurProcess;
import com.ufo.store.management.utils.Utils;
import com.ufo.store.management.widget.view.ObservableNestedScrollView;
import com.ufo.store.management.widget.PtrClassicFrameLayout.LoadingFooter;
import com.ufo.store.management.widget.PtrClassicFrameLayout.LoadingHeader;

import java.util.List;
import java.util.Map;

import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * Created by taoli on 2016/5/13.
 */
public class HomeShowFragment extends Fragment{
    private PtrClassicFrameLayout ptrClassicFrameLayout;
    private ObservableNestedScrollView mScrollView;
    private RecyclerView mRecyclerView;
    private Isszmbalv3StockOrMemberRecyclerViewAdapter mIsszmbalv3StockOrMemberRecyclerViewAdapter = null;
    private LinearLayout mNoStockMemberTipLy;

    private Map<String, String> mIsszmbalv3Map = null;
    private SerializableMap mIsszmbalv3StockTypeMap = null;
    private SerializableList mIsszmbalv3BranchInfoList = null;
    private SerializableList mIsszmbalv3SupcustInfoList = null;

    private TextView type_desc_tv;
    private TextView type_none_tv;
    private RecyclerView mTypeRecyclerView;
    private Isszmbalv3StockTypeRecyclerViewAdapter mIsszmbalv3StockTypeRecyclerViewAdapter;

    private int mDeletePosition = -1;

    private List<Isszmbalv3StockInfo> recentIsszmbalv3Stocks;
    private List<Isszmbalv3Member> recentIsszmbalv3Members;

    private JavaBlurProcess blurProcess = new JavaBlurProcess();
    private Bitmap mBgBitmap;
    private Bitmap overlay;
    private boolean isFlur = false;
    private int mScrollYMaxHeight = 110;   //控制栏高度不展开时是110dp
    private int mCurScrollYHeight = 0;
    private int forbidExhibitPos = -1;   //在item被detail页查看期间禁止应用动画翻转
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_home_show, container, false);
        ptrClassicFrameLayout = rootView.findViewById(R.id.ptr);
        mRecyclerView = (RecyclerView) rootView.findViewById(R.id.stock_member_recyclerview);
        mTypeRecyclerView = (RecyclerView) rootView.findViewById(R.id.type_recyclerview);
        type_desc_tv = (TextView) rootView.findViewById(R.id.type_desc_tv);
        type_none_tv = (TextView) rootView.findViewById(R.id.type_none_tv);
        mScrollView = (ObservableNestedScrollView) rootView.findViewById(R.id.layout_scroll);
        mNoStockMemberTipLy = (LinearLayout) rootView.findViewById(R.id.get_stockmember_fail_tip_ly);
        initialize();
        initData();
        return rootView;
    }

    private void initialize() {
        ptrClassicFrameLayout.disableWhenHorizontalMove(true);
        ptrClassicFrameLayout.setPtrHandler(ptrDefaultHandler2);//设置下拉上拉监听

        //设置头尾刷新布局（以下四步骤部必须按照顺序设置，否则无效）
        LoadingFooter ptrLoadUIHandler = new LoadingFooter(getContext());
        ptrClassicFrameLayout.setFooterView(ptrLoadUIHandler);
        ptrClassicFrameLayout.addPtrUIHandler(ptrLoadUIHandler);
        //添加头部处理功能
        LoadingHeader ptrRefreshUIHandler = new LoadingHeader(getContext());
        ptrClassicFrameLayout.addPtrUIHandler(ptrRefreshUIHandler);
        ptrClassicFrameLayout.setHeaderView(ptrRefreshUIHandler);

        //截取高斯模糊图
        mScrollView.setNestedScrollViewListener(new ObservableNestedScrollView.NestedScrollViewListener() {
            @Override
            public void onScrollChanged(ObservableNestedScrollView scrollView, int x, int y, int oldx, int oldy) {
                if (overlay != null && isFlur) {
                    mCurScrollYHeight = y;
                    processBlur();
                }
            }
        });

        mIsszmbalv3StockTypeRecyclerViewAdapter = new Isszmbalv3StockTypeRecyclerViewAdapter(getActivity(), mIsszmbalv3Map);
        mIsszmbalv3StockTypeRecyclerViewAdapter.setOnClickListener(new Isszmbalv3StockTypeRecyclerViewAdapter.OnClickListener() {
            @Override
            public void onItemClick(String showtype, String name) {
                Intent toIntent = new Intent(getActivity(), Isszmbalv3StockActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("Isszmbalv3StockType", mIsszmbalv3StockTypeMap);
                bundle.putString("ShowStockType", showtype);
                toIntent.putExtras(bundle);
                startActivity(toIntent );
            }

            @Override
            public void onItemLongClick( int position) {

            }
        });
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mTypeRecyclerView.setNestedScrollingEnabled(false);
        mTypeRecyclerView.setLayoutManager(linearLayoutManager);
        mTypeRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mTypeRecyclerView.setHasFixedSize(true);
        mTypeRecyclerView.setAdapter(mIsszmbalv3StockTypeRecyclerViewAdapter);

        mIsszmbalv3StockOrMemberRecyclerViewAdapter = new Isszmbalv3StockOrMemberRecyclerViewAdapter(getActivity(), mIsszmbalv3Map);
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setAdapter(mIsszmbalv3StockOrMemberRecyclerViewAdapter);
        mIsszmbalv3StockOrMemberRecyclerViewAdapter.setOnClickListener(new Isszmbalv3StockOrMemberRecyclerViewAdapter.OnClickListener() {
            @Override
            public void onDetailClick(int position) {
                Object obj = mIsszmbalv3StockOrMemberRecyclerViewAdapter.getItem(position);
                if (obj instanceof Isszmbalv3StockInfo) {
                    Isszmbalv3StockInfo isszmbalv3StockInfo = (Isszmbalv3StockInfo) obj;
                    Intent toIntent = new Intent(getActivity(), Isszmbalv3StockSalesActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("StockNo", isszmbalv3StockInfo.item_no);
                    bundle.putString("StockName", isszmbalv3StockInfo.item_name);
                    toIntent.putExtras(bundle);
                    startActivity(toIntent);
                } else if (obj instanceof Isszmbalv3Member) {
                    Isszmbalv3Member isszmbalv3MemberInfo = (Isszmbalv3Member) obj;
                    Intent toIntent = new Intent(getActivity(), Isszmbalv3MemberConsumptionActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("Isszmbalv3MemberInfo", isszmbalv3MemberInfo);
                    toIntent.putExtras(bundle);
                    startActivity(toIntent);
                }
                int i = mIsszmbalv3StockOrMemberRecyclerViewAdapter.getLongPressedIndex();
                if (i != -1) {
                    mIsszmbalv3StockOrMemberRecyclerViewAdapter.setLongPressedIndex(-1);
                    mIsszmbalv3StockOrMemberRecyclerViewAdapter.notifyItemChanged(i, "menu");
                }
            }

            @Override
            public void onDeleteClick(int position) {
                Object obj = mIsszmbalv3StockOrMemberRecyclerViewAdapter.getItem(position);
                if (obj instanceof Isszmbalv3StockInfo) {
                    Isszmbalv3StockInfo isszmbalv3StockInfo = (Isszmbalv3StockInfo) obj;
                    new DeleteIsszmbalv3StockInfoTask().execute(isszmbalv3StockInfo.item_no);
                } else if (obj instanceof Isszmbalv3Member) {
                    Isszmbalv3Member isszmbalv3MemberInfo = (Isszmbalv3Member) obj;
                    new DeleteIsszmbalv3MemberInfoTask().execute(isszmbalv3MemberInfo.memberCardId);
                }
                mDeletePosition = position;
                int i = mIsszmbalv3StockOrMemberRecyclerViewAdapter.getLongPressedIndex();
                if (i != -1) {
                    mIsszmbalv3StockOrMemberRecyclerViewAdapter.setLongPressedIndex(-1);
                    mIsszmbalv3StockOrMemberRecyclerViewAdapter.notifyItemChanged(i, "menu");
                }
            }

            @Override
            public void onItemClick(View v, int position) {
                Object obj = mIsszmbalv3StockOrMemberRecyclerViewAdapter.getItem(position);
                if (obj instanceof Isszmbalv3StockInfo) {
                    Isszmbalv3StockInfo isszmbalv3StockInfo = (Isszmbalv3StockInfo) obj;
                    if (mIsszmbalv3Map != null && mIsszmbalv3Map.size() > 1) {
                        Intent toIntent = new Intent(getActivity(), Isszmbalv3StockEditActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("Isszmbalv3StockInfo", isszmbalv3StockInfo);
                        bundle.putSerializable("Isszmbalv3StockType", mIsszmbalv3StockTypeMap);
                        bundle.putSerializable("Isszmbalv3SupcustInfo", mIsszmbalv3SupcustInfoList);
                        toIntent.putExtras(bundle);
                        // Now we can start the Activity, providing the activity options as a bundle
                        View view ;
                        if ( (( position == lastExhibitPos[0] ) && !bCanExhibit[0] )   ||  (( position == lastExhibitPos[1] ) && !bCanExhibit[1])) {
                            view = v.findViewById(R.id.stock_secondary_iv);
                        }else{
                            view = v.findViewById(R.id.stock_iv);
                        }
                        forbidExhibitPos = position;
                        /**
                         * Now create an {@link android.app.ActivityOptions} instance using the
                         * {@link ActivityOptionsCompat#makeSceneTransitionAnimation(Activity, Pair[])} factory
                         * method.
                         */
                        Pair<View, String> pContent = Pair.create(v.findViewById(R.id.stock_name_tv),Isszmbalv3StockEditActivity.VIW_NAME_HEAD_TOOLBAR);
                        Pair<View, String> pContent1 = Pair.create(v.findViewById(R.id.stock_rem_tv),Isszmbalv3StockEditActivity.VIEW_NAME_HEADER_TITLE);
                        Pair<View, String> pImage = Pair.create(view, Isszmbalv3StockEditActivity.VIEW_NAME_HEADER_IMAGE);
                        @SuppressWarnings("unchecked")
//                    ActivityOptionsCompat activityOptions = ActivityOptionsCompat.makeSceneTransitionAnimation(
//                            getActivity(),
//                            // Now we provide a list of Pair items which contain the view we can transitioning
//                            // from, and the name of the view it is transitioning to, in the launched activity
//                            pImage,pContent);
                                ActivityOptionsCompat activityOptions = ActivityOptionsCompat.makeSceneTransitionAnimation(
                                getActivity(),
                                // Now we provide a list of Pair items which contain the view we can transitioning
                                // from, and the name of the view it is transitioning to, in the launched activity
                                pImage,pContent,pContent1);
//                    @SuppressWarnings("unchecked")
//                    ActivityOptions activityOptions = ActivityOptions.makeSceneTransitionAnimation(
//                            getActivity(),
//                            // Now we provide a list of Pair items which contain the view we can transitioning
//                            // from, and the name of the view it is transitioning to, in the launched activity
//                            v.findViewById(R.id.icon_rl), Isszmbalv3StockEditActivity.VIEW_NAME_HEADER_IMAGE);
                        startActivity(toIntent, activityOptions.toBundle());
                    }
                } else if (obj instanceof Isszmbalv3Member) {
                    Isszmbalv3Member isszmbalv3MemberInfo = (Isszmbalv3Member) obj;
                    Intent toIntent = new Intent(getActivity(), Isszmbalv3MemberEditActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("Isszmbalv3MemberInfo", isszmbalv3MemberInfo);
                    toIntent.putExtras(bundle);
                    // Now we can start the Activity, providing the activity options as a bundle
                    View view ;
                    if ( (( position == lastExhibitPos[0] ) && !bCanExhibit[0] )   ||  (( position == lastExhibitPos[1] ) && !bCanExhibit[1])) {
                        view = v.findViewById(R.id.member_catalog_iv);
                    }else{
                        view = v.findViewById(R.id.member_icon_iv);
                    }
                    forbidExhibitPos = position;
                    /**
                     * Now create an {@link android.app.ActivityOptions} instance using the
                     * {@link ActivityOptionsCompat#makeSceneTransitionAnimation(Activity, Pair[])} factory
                     * method.
                     */
                    Pair<View, String> pContent = Pair.create(v.findViewById(R.id.member_name_tv), Isszmbalv3MemberEditActivity.VIEW_NAME_HEADER_TITLE);
                    Pair<View, String> pImage = Pair.create(view, Isszmbalv3MemberEditActivity.VIEW_NAME_HEADER_IMAGE);
                    @SuppressWarnings("unchecked")
//                ActivityOptionsCompat activityOptions = ActivityOptionsCompat.makeSceneTransitionAnimation(
//                        getActivity(),
//                        // Now we provide a list of Pair items which contain the view we can transitioning
//                        // from, and the name of the view it is transitioning to, in the launched activity
//                        pImage, pContent);
                            ActivityOptionsCompat activityOptions = ActivityOptionsCompat.makeSceneTransitionAnimation(
                            getActivity(),
                            // Now we provide a list of Pair items which contain the view we can transitioning
                            // from, and the name of the view it is transitioning to, in the launched activity
                            pImage, pContent);
//                    @SuppressWarnings("unchecked")
//                    ActivityOptions activityOptions = ActivityOptions.makeSceneTransitionAnimation(
//                            getActivity(),
//                            // Now we provide a list of Pair items which contain the view we can transitioning
//                            // from, and the name of the view it is transitioning to, in the launched activity
//                            v.findViewById(R.id.member_icon_iv), Isszmbalv3MemberEditActivity.VIEW_NAME_HEADER_IMAGE);
                    startActivity(toIntent, activityOptions.toBundle());
                }
                int i = mIsszmbalv3StockOrMemberRecyclerViewAdapter.getLongPressedIndex();
                if (i != -1) {
                    mIsszmbalv3StockOrMemberRecyclerViewAdapter.setLongPressedIndex(-1);
                    mIsszmbalv3StockOrMemberRecyclerViewAdapter.notifyItemChanged(i, "menu");
                }
            }

            @Override
            public void onItemLongClick(View v, int position) {
                int i = mIsszmbalv3StockOrMemberRecyclerViewAdapter.getLongPressedIndex();
                if (i != -1) {
                    mIsszmbalv3StockOrMemberRecyclerViewAdapter.setLongPressedIndex(position);
                    mIsszmbalv3StockOrMemberRecyclerViewAdapter.notifyItemChanged(i, "menu");
                    mIsszmbalv3StockOrMemberRecyclerViewAdapter.notifyItemChanged(position, "menu");
                } else {
                    mIsszmbalv3StockOrMemberRecyclerViewAdapter.setLongPressedIndex(position);
                    mIsszmbalv3StockOrMemberRecyclerViewAdapter.notifyItemChanged(position, "menu");
                }
            }

            @Override
            public void onMenuClick(View v, int position) {
                int i = mIsszmbalv3StockOrMemberRecyclerViewAdapter.getLongPressedIndex();
                if (i != -1) {
                    mIsszmbalv3StockOrMemberRecyclerViewAdapter.setLongPressedIndex(position);
                    mIsszmbalv3StockOrMemberRecyclerViewAdapter.notifyItemChanged(i, "menu");
                    mIsszmbalv3StockOrMemberRecyclerViewAdapter.notifyItemChanged(position, "menu");
                } else {
                    mIsszmbalv3StockOrMemberRecyclerViewAdapter.setLongPressedIndex(position);
                    mIsszmbalv3StockOrMemberRecyclerViewAdapter.notifyItemChanged(position, "menu");
                }
            }

            @Override
            public void onMaskClick(View v, int position) {
                int i = mIsszmbalv3StockOrMemberRecyclerViewAdapter.getLongPressedIndex();
                if (i != -1) {
                    mIsszmbalv3StockOrMemberRecyclerViewAdapter.setLongPressedIndex(-1);
                    mIsszmbalv3StockOrMemberRecyclerViewAdapter.notifyItemChanged(i, "menu");
                }
            }
        });
    }

    public void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                recentIsszmbalv3Stocks = DatabaseManager.getIsszmbalv3StockInfoListFromHistory(getActivity());
                recentIsszmbalv3Members = DatabaseManager.getIsszmbalv3MemberInfoListFromHistory(getActivity());
                mIsszmbalv3StockOrMemberRecyclerViewAdapter.clearData();
                //插入最近更改的商品
                for (Isszmbalv3StockInfo isszmbalv3StockInfo : recentIsszmbalv3Stocks)
                    mIsszmbalv3StockOrMemberRecyclerViewAdapter.insertData(isszmbalv3StockInfo);
                //插入分割页
                mIsszmbalv3StockOrMemberRecyclerViewAdapter.insertData(new Object());
                //插入最近更改的会员
                for (Isszmbalv3Member isszmbalv3MemberInfo : recentIsszmbalv3Members)
                    mIsszmbalv3StockOrMemberRecyclerViewAdapter.insertData(isszmbalv3MemberInfo);
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mIsszmbalv3StockOrMemberRecyclerViewAdapter.notifyDataSetChanged();
                        mScrollView.smoothScrollTo(0, 0);
                        if (recentIsszmbalv3Stocks.size() + recentIsszmbalv3Members.size() == 0) {
                            mRecyclerView.setVisibility(View.GONE);
                            mNoStockMemberTipLy.setVisibility(View.VISIBLE);
                        } else {
                            mRecyclerView.setVisibility(View.VISIBLE);
                            mNoStockMemberTipLy.setVisibility(View.GONE);
                            startAnimateExhibit();
                        }

                        //制作高斯模糊图
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                // 首先对ImageView进行截图并保存为位图
                                mBgBitmap = BitmapUtils.shotNestedScrollView(mScrollView);
                                int scaleFactor = 6;
                                if (overlay != null) {
                                    overlay.recycle();
                                }
                                overlay = Bitmap.createScaledBitmap(mBgBitmap, mBgBitmap.getWidth() / scaleFactor, mBgBitmap.getHeight() / scaleFactor, false);
                                overlay = blurProcess.blur(overlay, 6);//高斯模糊
                                overlay = Bitmap.createScaledBitmap(overlay, mBgBitmap.getWidth(), mBgBitmap.getHeight(), false);
                                isFlur = true;
                            }
                        }, 700);
                    }
                });
            }
        }).start();
    }

    private boolean pullRefresh = true;

    public void setPullRefresh(boolean pullRefresh) {
        this.pullRefresh = pullRefresh;
    }

    private boolean loadMore = false;

    public void setLoadMore(boolean loadMore) {
        this.loadMore = loadMore;
    }

    private PtrDefaultHandler2 ptrDefaultHandler2 = new PtrDefaultHandler2() {
        @Override
        public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
            return pullRefresh && PtrDefaultHandler2.checkContentCanBePulledDown(frame, mScrollView, header);
        }

        @Override
        public void onRefreshBegin(PtrFrameLayout frame) {
            initData();
            ptrClassicFrameLayout.postDelayed(new Runnable() {
                @Override
                public void run() {
                    ptrClassicFrameLayout.refreshComplete();
                }
            }, 1000);
        }

        @Override
        public boolean checkCanDoLoadMore(PtrFrameLayout frame, View content, View footer) {
            return loadMore && PtrDefaultHandler2.checkContentCanBePulledUp(frame, mScrollView, footer);
        }

        @Override
        public void onLoadMoreBegin(PtrFrameLayout frame) {
            ptrClassicFrameLayout.postDelayed(new Runnable() {
                @Override
                public void run() {
                    ptrClassicFrameLayout.refreshComplete();
                }
            }, 0);
        }
    };

    private void processBlur() {
        if (mCurScrollYHeight > 0) {
            if (mCurScrollYHeight > (int) Utils.sp2px(getResources(), mScrollYMaxHeight)) {
                Bitmap bitmap1 = Bitmap.createBitmap(overlay, 0, mCurScrollYHeight - (int) Utils.sp2px(getResources(), mScrollYMaxHeight), overlay.getWidth(), (int) Utils.sp2px(getResources(), mScrollYMaxHeight));
                if (mFlurListener != null) {
                    mFlurListener.onFlurChanged(bitmap1);
                }
            } else {
                Bitmap bitmap1 = Bitmap.createBitmap(overlay, 0, 1, overlay.getWidth(), mCurScrollYHeight);
                if (mFlurListener != null) {
                    mFlurListener.onFlurChanged(bitmap1);
                }
            }
        } else {
            if (mFlurListener != null) {
                mFlurListener.onFlurChanged(null);
            }
        }
    }

    public void setScrollYMaxHeight(int controllerHeight) {
        mScrollYMaxHeight = controllerHeight;
        processBlur();
    }

    public void setIsszmbalv3StockTypeMap(SerializableMap stockTypeMap) {
        mIsszmbalv3StockTypeMap = stockTypeMap;
        mIsszmbalv3Map = stockTypeMap.getMap();
        if (mIsszmbalv3Map != null && mIsszmbalv3Map.size() > 1) {
            type_desc_tv.setText("全部 " + String.valueOf(mIsszmbalv3Map.size() - 1));
            type_none_tv.setVisibility(View.GONE);
            mTypeRecyclerView.setVisibility(View.VISIBLE);
            if (mIsszmbalv3StockOrMemberRecyclerViewAdapter != null && mIsszmbalv3StockOrMemberRecyclerViewAdapter.getMap() == null) {
                mIsszmbalv3StockOrMemberRecyclerViewAdapter.setMap(mIsszmbalv3Map);
                mIsszmbalv3StockOrMemberRecyclerViewAdapter.notifyDataSetChanged();
            }
            if (mIsszmbalv3StockTypeRecyclerViewAdapter != null && mIsszmbalv3StockTypeRecyclerViewAdapter.getMap().size() == 0) {
                mIsszmbalv3StockTypeRecyclerViewAdapter.setMap(mIsszmbalv3Map);
                mIsszmbalv3StockTypeRecyclerViewAdapter.notifyDataSetChanged();
                startAnimateRolling();
            }
        }
    }

    public void setIsszmbalv3BranchInfoList(SerializableList branchInfoList){
        mIsszmbalv3BranchInfoList = branchInfoList;
    }

    public void setIsszmbalv3SupcustInfoList(SerializableList supcustInfoList){
        mIsszmbalv3SupcustInfoList = supcustInfoList;
    }

    /**
     * @desc 商品类型水平滚动，滚动到尾部时自动回到头部
     * @author taoli
     * @time 2017/8/25 0025
     */
    private CountDownTimer horizontalScrollCountDownTimer;
    public void startAnimateRolling() {
        if( horizontalScrollCountDownTimer == null ) {
            horizontalScrollCountDownTimer = new CountDownTimer(Long.MAX_VALUE, 30) {
                @Override
                public void onTick(long millisUntilFinished) {
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mTypeRecyclerView.scrollBy(1, 0);
                            if( isRecyclerViewSlideToBottom(mTypeRecyclerView)){
                                mTypeRecyclerView.smoothScrollToPosition(0);
                            }
                        }
                    });
                }

                @Override
                public void onFinish() {

                }
            }.start();
        }else{
            horizontalScrollCountDownTimer.start();
        }
    }

    public void pauseAnimateRolling(){
        if( horizontalScrollCountDownTimer != null )
            horizontalScrollCountDownTimer.cancel();
    }

    /**
     * @desc 商品图片垂直滚动展示，会员图片水平翻转展示
     * @author taoli
     * @time 2017/8/25 0025
     */
    private boolean bCanExhibit[] = {true, true};
    private int lastExhibitPos[] = {-1, -1};
    private CountDownTimer firstVerticalScrollCountDownTimer;
    private CountDownTimer secondVerticalScrollCountDownTimer;
    public void startAnimateExhibit() {
        forbidExhibitPos = -1;
        if( firstVerticalScrollCountDownTimer == null ) {
            firstVerticalScrollCountDownTimer = new CountDownTimer(Long.MAX_VALUE, 4500) {
                @Override
                public void onTick(long millisUntilFinished) {
                    int lastVisibleItemPosition = getRecyclerViewLastVisibleItemPosition(mRecyclerView);
                    int result = lastVisibleItemPosition - new java.util.Random().nextInt(5);
                    if ( ( result == lastExhibitPos[1] && bCanExhibit[0] ) || ( result == forbidExhibitPos ) ){
                        return;
                    }
                    if (result >= 0 && result <= lastVisibleItemPosition) {
                        if (bCanExhibit[0]) {
                            bCanExhibit[0] = false;
                            lastExhibitPos[0] = result;
                            mIsszmbalv3StockOrMemberRecyclerViewAdapter.notifyItemChanged(lastExhibitPos[0], "exhibit");
                        } else {
                            mIsszmbalv3StockOrMemberRecyclerViewAdapter.notifyItemChanged(lastExhibitPos[0], "recovery");
                            bCanExhibit[0] = true;
                            lastExhibitPos[0] = -1;
                        }
                    }
                }

                @Override
                public void onFinish() {

                }
            }.start();
        }else{
            firstVerticalScrollCountDownTimer.start();
        }
        if( secondVerticalScrollCountDownTimer == null ) {
            secondVerticalScrollCountDownTimer = new CountDownTimer(Long.MAX_VALUE, 4000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    int lastVisibleItemPosition = getRecyclerViewLastVisibleItemPosition(mRecyclerView);
                    int result = lastVisibleItemPosition - new java.util.Random().nextInt(5);
                    if ( ( result == lastExhibitPos[0] && bCanExhibit[1]) || ( result == forbidExhibitPos ) ) {
                        return;
                    }
                    if (result >= 0 && result <= lastVisibleItemPosition) {
                        if (bCanExhibit[1]) {
                            bCanExhibit[1] = false;
                            lastExhibitPos[1] = result;
                            mIsszmbalv3StockOrMemberRecyclerViewAdapter.notifyItemChanged(lastExhibitPos[1], "exhibit");
                        } else {
                            mIsszmbalv3StockOrMemberRecyclerViewAdapter.notifyItemChanged(lastExhibitPos[1], "recovery");
                            bCanExhibit[1] = true;
                            lastExhibitPos[1] = -1;
                        }
                    }
                }

                @Override
                public void onFinish() {

                }
            }.start();
        }else{
            secondVerticalScrollCountDownTimer.start();
        }
    }

    public void pauseAnimateExhibit() {
        if( firstVerticalScrollCountDownTimer != null ) {
            firstVerticalScrollCountDownTimer.cancel();
        }
        if( secondVerticalScrollCountDownTimer != null ) {
            secondVerticalScrollCountDownTimer.cancel();
        }
    }

    public boolean isRecyclerViewSlideToBottom(RecyclerView recyclerView) {
        if (recyclerView == null)
            return false;
        if (recyclerView.computeHorizontalScrollExtent() + recyclerView.computeHorizontalScrollOffset() >= recyclerView.computeHorizontalScrollRange())
            return true;
        return false;
    }

    private int getRecyclerViewLastVisibleItemPosition(RecyclerView recyclerView) {
        int lastVisibleItemPosition = 0;
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof LinearLayoutManager) {
            lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
        } else if (layoutManager instanceof GridLayoutManager) {
            lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
            int[] lastPositions = new int[staggeredGridLayoutManager.getSpanCount()];
            staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions);
            lastVisibleItemPosition = Utils.findMax(lastPositions);
        }
        return lastVisibleItemPosition;
    }

    private class DeleteIsszmbalv3StockInfoTask extends AsyncTask<String, Void, String> {
        public DeleteIsszmbalv3StockInfoTask() {
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... params) {
            String stockno = params[0];
            long row = DatabaseManager.deleteIsszmbalv3StockInfoFromHistory(getActivity(), stockno);
            return String.valueOf(row);
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            if (result.equals("1")) {
                mIsszmbalv3StockOrMemberRecyclerViewAdapter.removeData(mDeletePosition);
                mIsszmbalv3StockOrMemberRecyclerViewAdapter.notifyItemRemoved(mDeletePosition);
            }
        }
    }

    private class DeleteIsszmbalv3MemberInfoTask extends AsyncTask<String, Void, String> {
        public DeleteIsszmbalv3MemberInfoTask() {
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... params) {
            String cardId = params[0];
            long row = DatabaseManager.deleteIsszmbalv3MemberInfoFromHistory(getActivity(), cardId);
            return String.valueOf(row);
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            if (result.equals("1")) {
                mIsszmbalv3StockOrMemberRecyclerViewAdapter.removeData(mDeletePosition);
                mIsszmbalv3StockOrMemberRecyclerViewAdapter.notifyItemRemoved(mDeletePosition);
            }
        }
    }

    private OnFlurListener mFlurListener = null;

    public void setFlurListener(OnFlurListener listener) {
        mFlurListener = listener;
    }

    public interface OnFlurListener {
        void onFlurChanged(Bitmap bg);
    }
}
